package com.test1;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @Author:zxh
 * @Description:
 * @Date: 2020/10/10
 * @Modified By:
 */
public class test {

   static class singleton {
        private singleton(){

        }
        private static  singleton singletonO = null;
        public singleton getSingleton(){
            if (singletonO == null) {
              synchronized (singleton.class){
                if (singletonO == null) {
                    singletonO = new singleton();
                }
              }
            }

            return singletonO;
        }
    }

    /**
     * 二分查找
     *
     * @param args
     */
    public static int twoSearch(int[] arr, int a) {
        int mid;
        int start = 0;
        int end = arr.length - 1;
        while (start <= end) {
            mid = (end + start) / 2;
            if (a > arr[mid]) {
                start = mid + 1;
            } else if (a < arr[mid]) {
                end = mid - 1;
            } else {//向左查找
                return mid + 1;
            }
        }
        return -1;
    }

    /**
     * 冒泡排序
     *
     * @param arr
     * @return
     */
    public static int[] sort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return arr;
    }
    public static int[] bubble(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            // 设定一个标记，若为true，则表示此次循环没有进行交换，也就是待排序列已经有序，排序已经完成。
            boolean flag = true;

            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;

                   // flag = false;
                }
            }


        }
        return arr;
    }







    /**
     * 二分查找递归实现。
     * @param srcArray  有序数组
     * @param start 数组低地址下标
     * @param end   数组高地址下标
     * @param key  查找元素
     * @return 查找元素不存在返回-1
     */
    public static int binSearch(int srcArray[], int start, int end, int key) {
        int mid = (end - start) / 2 + start;
        if (srcArray[mid] == key) {
            return mid;
        }
        if (start >= end) {
            return -1;
        } else if (key > srcArray[mid]) {
            return binSearch(srcArray, mid + 1, end, key);
        } else if (key < srcArray[mid]) {
            return binSearch(srcArray, start, mid - 1, key);
        }
        return -1;
    }

        public static void main(String[] args) {
        int[] arr = {2,5,3,8,4,6,9,1};
        int[] bubble = sort(arr);
        int i = twoSearch(bubble, 5);
        System.out.println("输出冒泡排序："+i);
        System.out.println("7/2: "+ 7/2);

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("23",34);
        jsonObject.put("45",67);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("33",12);
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        System.out.print(jsonArray.toString());

        String str = "23   w  ew we";
        String s = str.replaceAll(" ", "");
        System.out.println("输出: "+s);

        String creditRatingAgency = "'1','16','17','18'";
        String[] strOne = {"1","12"};
        String collect = Arrays.stream(strOne).collect(Collectors.joining(","));
        System.out.println("collent:"+strOne.toString());

    }
}
